﻿using System;
using System.Linq;
using System.Reflection;

namespace com.hyrentals.common
{
    public static class AttributeHelper
    {
        /// <summary>
        /// 扩展方法：获得反射对象相关的（0或1个）指定的Attribute
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="provider"></param>
        /// <param name="inherit"></param>
        /// <returns></returns>
        public static T GetCustomAttribute<T>(this ICustomAttributeProvider provider, bool inherit = false)
            where T : Attribute
        {
            return provider.GetCustomAttributes(typeof(T), inherit)
                .FirstOrDefault() as T;
        }
    }
}
